View Javadoc
1 /* ====================================================================
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>;.
53 *
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
57 */
58
59 package org.troublescope.evaluation;
60
61 import java.lang.reflect.Method;
62 import java.lang.reflect.Modifier;
63 import java.util.*;
64
65 import junit.framework.Test;
66 import junit.framework.TestResult;
67
68 import org.apache.commons.logging.Log;
69 import org.apache.commons.logging.LogFactory;
70
71 import org.troublescope.ApplicationContext;
72 import org.troublescope.ContextAware;
73 import org.troublescope.util.NameValuePair;
74
75 /***
76 * A test suite.
77 */
78 public class TestSuite
79 {
80
81 private Log log = LogFactory.getLog(TestSuite.class);
82
83 private String name;
84 private String description;
85 private String resolutionName;
86 private String testSuiteClassName;
87 private TestSuiteConfig config;
88
89 /***
90 * Create a new <code>TestSuite</code>.
91 */
92 public TestSuite()
93 {
94 config = new TestSuiteConfig();
95 }
96
97 /***
98 * Returns the name.
99 */
100 public String getName()
101 {
102 return name;
103 }
104
105 /***
106 * Sets the name.
107 */
108 public void setName(String aName)
109 {
110 name = aName;
111 }
112
113 /***
114 * Returns the description.
115 */
116 public String getDescription()
117 {
118 return description;
119 }
120
121 /***
122 * Sets the description.
123 */
124 public void setDescription(String aDescription)
125 {
126 if (aDescription != null) {
127 description = aDescription.trim();
128 } else {
129 description = aDescription;
130 }
131 }
132
133 /***
134 * Returns the result id.
135 */
136 public String getResolutionName()
137 {
138 return resolutionName;
139 }
140
141 /***
142 * Set the result id.
143 */
144 public void setResolutionName(String aResolutionName)
145 {
146 resolutionName = aResolutionName;
147 }
148
149 /***
150 * Returns a list of init parameters.
151 */
152 public Iterator getInitParameters()
153 {
154 return config.getInitParameterList().iterator();
155 }
156
157 /***
158 * Add a parameter.
159 */
160 public void addInitParameter(NameValuePair param)
161 {
162 config.addInitParameter(param);
163 }
164
165 /***
166 * Returns the test suite builder.
167 */
168 public String getTestSuiteClassName()
169 {
170 return testSuiteClassName;
171 }
172
173 /***
174 * Set the test suite builder.
175 */
176 public void setTestSuiteClassName(String aClassName)
177 {
178 testSuiteClassName = aClassName;
179 }
180
181 /***
182 * Start the test suite.
183 */
184 public TestResult start(ApplicationContext ctx)
185 throws EvaluationException
186 {
187 junit.framework.TestSuite suite = createTestSuite(ctx);
188 TestResult result = new TestResult();
189 suite.run(result);
190 return result;
191 }
192
193 /***
194 * Returns a configuration for this suite.
195 */
196 public TestSuiteConfig getConfig()
197 {
198 return config;
199 }
200
201 /***
202 * Returns the actual test suite.
203 */
204 public junit.framework.TestSuite getActualSuite(ApplicationContext ctx)
205 throws EvaluationException
206 {
207 return createTestSuite(ctx);
208 }
209
210 /***
211 * Create a test suite instance.
212 *
213 * @task Support specifying class loaders for loading test suites.
214 */
215 private junit.framework.TestSuite createTestSuite(ApplicationContext context)
216 throws EvaluationException
217 {
218 try {
219 Class c = Class.forName(getTestSuiteClassName());
220 junit.framework.TestSuite suite = null;
221 if (junit.framework.TestSuite.class.isAssignableFrom(c)) {
222 log.debug("Creating suite from class name");
223 suite = (junit.framework.TestSuite) c.newInstance();
224 } else {
225 log.debug("Creating suite from suite method");
226 suite = getSuiteFromSuiteMethod(c);
227 }
228 initTestSuite(suite, context);
229 return suite;
230 } catch (EvaluationException e) {
231 throw e;
232 } catch (Exception e) {
233 throw new EvaluationException("Error create test suite: " + getTestSuiteClassName(), e);
234 }
235 }
236
237 /***
238 * Try to load the test suite using the <code>suite()</code> method.
239 */
240 private junit.framework.TestSuite getSuiteFromSuiteMethod(Class c)
241 throws EvaluationException
242 {
243 try {
244 Method suiteMethod = c.getMethod("suite", null);
245 if (!Modifier.isStatic(suiteMethod.getModifiers())) {
246 throw new EvaluationException("Suite method in " + c.getName() + " is not static");
247 }
248 if (!junit.framework.TestSuite.class.isAssignableFrom(suiteMethod.getReturnType())) {
249 throw new EvaluationException("Suite method in " + c.getName()
250 + " does not return an instance of " + junit.framework.TestSuite.class.getName());
251 }
252 return (junit.framework.TestSuite) suiteMethod.invoke(null, null);
253 } catch (Exception e) {
254 throw new EvaluationException("Could not find suite() method in class " + c.getName(), e);
255 }
256 }
257
258 /***
259 * Initializing test suite.
260 */
261 private void initTestSuite(junit.framework.TestSuite suite, ApplicationContext context)
262 {
263 Enumeration tests = suite.tests();
264 while (tests.hasMoreElements()) {
265 Test test = (Test) tests.nextElement();
266 if (test instanceof junit.framework.TestSuite) {
267 initTestSuite((junit.framework.TestSuite) test, context);
268 }
269 if (test instanceof EvaluationTest) {
270 log.debug("Setting config...");
271 ((EvaluationTest) test).setConfig(config);
272 }
273 if (test instanceof ContextAware) {
274 log.debug("Setting context...");
275 ((ContextAware) test).setContext(context);
276 }
277 }
278 }
279
280 }
This page was automatically generated by Maven